package com.doctor.windflower_doctor.http.request;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import android.os.AsyncTask;

import com.doctor.windflower_doctor.http.callback.HttpSpringCallBack;
import com.lidroid.xutils.exception.HttpException;

/**
 * <p>
 * 功能
 * </p>
 * 
 * <p>
 * Copyright 北京亚普拓信息咨询服务有限公司 2014 All right reserved.
 * </p>
 * 
 * @author 皮勉 时间 2014年10月8日 下午5:18:59
 * @version 1.0 </br> 最后修改人 无
 * 
 */
public class UploadRequestTask<T> extends AsyncTask<Void, Void, Object> {

	private static RestTemplate restTemplate;
	private String uploadPath = null;
	private Map<String, String> bodyParams;
	private Map<String, String> fileParams;
	private HttpSpringCallBack<T> callBack;

	static {
		restTemplate = new RestTemplate();
		ClientHttpRequestFactory factory = restTemplate.getRequestFactory();
		if (factory instanceof SimpleClientHttpRequestFactory) {
			((SimpleClientHttpRequestFactory) factory).setConnectTimeout(30 * 1000);
			((SimpleClientHttpRequestFactory) factory).setReadTimeout(30 * 1000);
		} else if (factory instanceof HttpComponentsClientHttpRequestFactory) {

			((HttpComponentsClientHttpRequestFactory) factory).setReadTimeout(30 * 1000);
			((HttpComponentsClientHttpRequestFactory) factory).setConnectTimeout(30 * 1000);
		}
	}

	public UploadRequestTask(String uploadPath, Map<String, String> bodyParams, Map<String, String> fileParams, HttpSpringCallBack<T> callBack) {
		super();
		this.uploadPath = uploadPath;
		this.bodyParams = bodyParams;
		this.fileParams = fileParams;
		this.callBack = callBack;
	}

	//
	// public void setParser(Parser parser, Class T){
	// this.parser = parser;
	// this.targetObj = T;
	// }

	@Override
	protected Object doInBackground(Void... params) {
		try {
			MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
			Resource resource;
			if (bodyParams != null) {
				Set<String> keySet = bodyParams.keySet();
				for (String key : keySet) {
					formData.add(key, new String(bodyParams.get(key).getBytes(), "iso-8859-1"));
				}
			}
			if (fileParams != null) {
				Set<String> keySet = fileParams.keySet();
				for (String key : keySet) {
					resource = new UrlResource(fileParams.get(key));
					formData.add(key, resource);
				}
			}

			HttpHeaders requestHeaders = new HttpHeaders();
			requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
			HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(formData, requestHeaders);
			List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
			list.add(new FormHttpMessageConverter());
			list.add(new StringHttpMessageConverter());
			restTemplate.setMessageConverters(list);
			ResponseEntity<String> response = restTemplate.exchange(uploadPath, HttpMethod.POST, requestEntity, String.class);
			// String jsonStr = response.getBody();
			// if(parser != null){
			// T target = (T) parser.parserResult(jsonStr, targetObj);
			// LogUtils.i("TAG", response.getBody());
			callBack.onSuccess(response);
			return response;
			// }else{
			// callBack.onSuccess();
			// return (T)jsonStr;
			// }
		} catch (RestClientException e) {

			callBack.onFailure(new HttpException(e), "网络不给力！");
			e.printStackTrace();

		} catch (MalformedURLException e) {

			callBack.onFailure(new HttpException(e), "网络地址异常！");
			e.printStackTrace();

		} catch (UnsupportedEncodingException e) {

			callBack.onFailure(new HttpException(e), "网络编码异常！");
			e.printStackTrace();

		}
		return null;
	}

}
